package com.jmx.yp.struts.member;

import net.sf.json.JSONObject;

import org.apache.log4j.*;
import org.apache.struts2.ServletActionContext;

import com.jmx.yp.common.SysConstants;
import com.jmx.yp.entity.GroupEmployee;
import com.jmx.yp.service.group.IGroupinfoService;
import com.jmx.yp.service.member.IMemberService;
import com.jmx.yp.service.ICommonService; 
import com.opensymphony.xwork2.ActionContext;

import java.io.PrintWriter;
import java.util.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 功能描述：
 *			会员管理业务控制
 * @author Jenny
 *
 * Date:2011-2-18 下午03:42:47
 */
public class MemberAction {
	private static Logger log = (Logger)Logger.getLogger(MemberAction.class);
	
	private IMemberService memberService;
	
	private ICommonService commonService;
	
	private IGroupinfoService groupService;
	
	public void setMemberService(IMemberService memberService){
		this.memberService = memberService;
	}
	
	public void setCommonService(ICommonService commonService){
		this.commonService = commonService;
	}
	
	public void setGroupService(IGroupinfoService groupService){
		this.groupService = groupService;
	}
	
	/**
	 * 查询会员：入口
	 * @return
	 */
	public String initMemberList(){
		return "queryMember";
	}
	
	/**
	 * 新增会员：入口
	 * 	   说明：
	 * 		如果是超级管理员及JMX管理员新增用户，则所属区域是在新增框里面进行选择
	 * 		如果是组织机构管理员，则直接通过下接框进行查询
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String initAddMember(){
		GroupEmployee employee = (GroupEmployee)ActionContext.getContext().getSession().get("user");
		
		try{
			//界面初始化数据
			Map paramMap = new HashMap();
			paramMap.put("paramType", SysConstants.MEMBER_TYPE);			
			memberTypeList = commonService.getSystemParam(paramMap); //会员类型
			
			//公司规模
			paramMap.clear();
			paramMap.put("paramType", SysConstants.COMPANY_SIZE);
			paramMap.put("orderFlag", true);
			companySizeList = commonService.getSystemParam(paramMap);
			
			//所属行业
			paramMap.put("paramType", SysConstants.INDUSTRY);
			industryList = commonService.getSystemParam(paramMap);
			
			//公司类型
			paramMap.put("paramType", SysConstants.COMPANY_TYPE);
			companyTypeList = commonService.getSystemParam(paramMap);
			
			educationMap = SysConstants.getEducationMap();  //学历
			
			//判断是否是JMX管理员
			if(employee.getGroup_srl() != null){
				jmxManager = groupService.isBlongToJMX(employee.getGroup_srl());
				if(!jmxManager){
					//查询组织机构绑定的组织，直接以下接框方式显示
					areaList = memberService.getAreaByGroupId(employee.getGroup_srl());
				}
			}
				
			
			
		}catch(Exception e){
			e.printStackTrace();
			log.error("MemberAction initAddMember error: " + e.getMessage());
		}
		return "addMember";
	}
	
	/**
	 * 查询区域：入口
	 * 	       说明：当超级管理员及JMX管理员时，可以查询所有的区域
	 * 		            机构管理员只允许查询与该机构绑定的区域
	 * @return
	 */
	public String initSearchArea(){
		return "searchArea";
	}
	
	/**
	 * 查询区域
	 * 	  备注：针对超级管理员及JMX管理员
	 * @return
	 */
	public String searchArea(){
		String xmlDoc = "";
		
		try{			
			xmlDoc = memberService.getAreaList(queryAreaName, null).asXML();
		
		System.out.println(xmlDoc);
		
			HttpServletResponse response = ServletActionContext.getResponse();
	        response.setContentType("text/xml;charset=UTF-8");   
	        response.setHeader("Cache-Control","no-cache");
	        PrintWriter out = response.getWriter();  
	        
	        out.write(xmlDoc); 
	        out.flush();			
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 加载子区域
	 * @return
	 */
	public String loadArea(){
		try{
			Map map = memberService.getSubAreaList(loadAreaId);
			
			result = JSONObject.fromObject(map);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "success";
	}
	
	/*初始化列表信息*/
	private List memberTypeList;  //会员类型
	private Map<Integer, String> educationMap;  //学历
	private List companySizeList; //公司规模
	private List industryList;  //行业
	private List companyTypeList; //公司类型
	private boolean jmxManager = true;
	private List areaList;
	
	private JSONObject result;
	
	//区域查询表单
	private String queryAreaName;
	private Long loadAreaId;

	public Long getLoadAreaId() {
		return loadAreaId;
	}

	public void setLoadAreaId(Long loadAreaId) {
		this.loadAreaId = loadAreaId;
	}

	public List getMemberTypeList() {
		return memberTypeList;
	}

	public void setMemberTypeList(List memberTypeList) {
		this.memberTypeList = memberTypeList;
	}

	public Map<Integer, String> getEducationMap() {
		return educationMap;
	}

	public void setEducationMap(Map<Integer, String> educationMap) {
		this.educationMap = educationMap;
	}

	public List getCompanySizeList() {
		return companySizeList;
	}

	public void setCompanySizeList(List companySizeList) {
		this.companySizeList = companySizeList;
	}

	public List getIndustryList() {
		return industryList;
	}

	public void setIndustryList(List industryList) {
		this.industryList = industryList;
	}

	public List getCompanyTypeList() {
		return companyTypeList;
	}

	public void setCompanyTypeList(List companyTypeList) {
		this.companyTypeList = companyTypeList;
	}

	public boolean getJmxManager() {
		return jmxManager;
	}

	public void setJmxManager(boolean jmxManager) {
		this.jmxManager = jmxManager;
	}

	public List getAreaList() {
		return areaList;
	}

	public void setAreaList(List areaList) {
		this.areaList = areaList;
	}

	public String getQueryAreaName() {
		return queryAreaName;
	}

	public void setQueryAreaName(String queryAreaName) {
		this.queryAreaName = queryAreaName;
	}

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}


	
	
	
	
}
